$( function() {
  const baseURL = 'http://localhost:8080/api/v1'

  $.ajaxSetup({
      beforeSend: (xhr) => {
        //   获取本地存储中的token
          const token = localStorage.getItem('token')
          xhr.setRequestHeader('Authorization',token)
      },

      error: (xhr,status,error) => {
          console.log('请求失败了');
          console.log(xhr, status, error);
          if (error === 'Forbidden') {
              alert('登录状态失效,请重新登录')
              location.href == './login.html'
          }
      }
  })
  $.ajax({
    method: 'get',
    url: baseURL + '/admin/user/info',
    success: (res) => {
        console.log(res);
        // 使用解构语法 把nickname userpic解构出来
        const { nickname, userPic } = res.data
        $('.user_info span ').text('欢迎' + nickname)
        // JQ 可以支持并级选择器 同时选中两个图片元素修改src 属性
        $('.user_info img , .user_center_link img').prop({ src: userPic })
      
    }
  })

  $('.logout').click(()=>{
      localStorage.removeItem('token')
      location.href = './login.html'
  })
})